{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> TRAIN SCHEME </h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "start1 = time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n",
    "from skimage.feature import local_binary_pattern\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import KFold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>2. Deklarasi Fungsi Load Dataset</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_dataset(folderdataset):\n",
    "    image = []\n",
    "    label = []\n",
    "    folders = os.listdir(os.path.dirname(folderdataset))\n",
    "    noLabel = 0\n",
    "    grannysmith = 0\n",
    "    for f in folders:\n",
    "        if f!='.DS_Store':\n",
    "            path = folderdataset+f\n",
    "            for filename in os.listdir(path):\n",
    "                if filename !='.DS_Store':\n",
    "                    file = path+'/'+filename\n",
    "                    img = np.asarray(cv2.imread(file))\n",
    "                    image.append(img)\n",
    "                    label.append(f)\n",
    "#         noLabel = noLabel+1\n",
    "    return image,label"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>3. Deklarasi Fungsi Preprocessing</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocessing_texture(array):\n",
    "    preprocessed=[]\n",
    "    kernel = np.ones((3,3),np.uint8)\n",
    "    for i in range(np.shape(array)[0]):\n",
    "        image=cv2.cvtColor(array[i], cv2.COLOR_BGR2GRAY)\n",
    "        img = image.copy()\n",
    "        thresh_val, img_th = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY_INV)\n",
    "        img_th = cv2.morphologyEx(img_th, cv2.MORPH_CLOSE, kernel)\n",
    "        img_th = cv2.erode(img_th,kernel,iterations = 2)\n",
    "        img_th2 = img_th\n",
    "        valid_range = np.logical_and(img_th,img_th2)\n",
    "        img[np.logical_not(valid_range)] =0\n",
    "        preprocessed.append(img)\n",
    "    return preprocessed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocessing_shape(array):\n",
    "    preprocessed=[]\n",
    "    for i in range(np.shape(array)[0]):\n",
    "        img=cv2.cvtColor(array[i], cv2.COLOR_BGR2GRAY)\n",
    "        ret,thresh = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)\n",
    "        preprocessed.append(thresh)\n",
    "    return preprocessed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def preprocessing_color(array):\n",
    "    preprocessed=[]\n",
    "    kernel = np.ones((3,3),np.uint8)\n",
    "    \n",
    "    for i in range(np.shape(array)[0]):\n",
    "        img = array[i].copy()\n",
    "        threshold = [250,250,255]\n",
    "\n",
    "        red = (threshold[2] > img[:,:,2])\n",
    "        green = (threshold[1] > img[:,:,1])\n",
    "        blue = (threshold[0] > img[:,:,0])\n",
    "\n",
    "        valid_range = np.logical_and(red,green,blue)\n",
    "        img[np.logical_not(valid_range)] = 0\n",
    "        img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)\n",
    "        img = cv2.erode(img,kernel,iterations = 3)\n",
    "        img=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "        preprocessed.append(img)\n",
    "    return preprocessed"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>4. Deklarasi Fungsi Ekstraksi Fitur</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_texture(img):\n",
    "    radius = 1\n",
    "    n_points = 8 * radius\n",
    "    \n",
    "    lbp = local_binary_pattern(img, n_points, radius, method='uniform')\n",
    "    n_bins = int(lbp.max() + 1)\n",
    "    hist, _ = np.histogram(lbp.ravel(), bins=n_bins, range=(0, n_bins))\n",
    "    hist = hist.astype(\"float\")                         \n",
    "    hist /= (hist.sum() + (1e-7))\n",
    "    return hist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_shape(img):\n",
    "    moments = cv2.moments(img)\n",
    "    huMoment = cv2.HuMoments(moments)\n",
    "    for i in range(0,7):\n",
    "        huMoment[i] = -1* np.copysign(1.0, huMoment[i]) * np.log10(abs(huMoment[i]))\n",
    "    return huMoment.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_color(img):\n",
    "    features = []\n",
    "\n",
    "    hist = cv2.calcHist([img], [0], None, [24], [0, 180])\n",
    "                        \n",
    "    return np.array(hist).flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def featureextraction_texture(array):\n",
    "    arr_feature = []\n",
    "    for i in range(np.shape(array)[0]):\n",
    "        arr_feature.append(extract_texture(array[i]))\n",
    "    return arr_feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def featureextraction_shape(array):\n",
    "    arr_feature = []\n",
    "    for i in range(np.shape(array)[0]):\n",
    "        arr_feature.append(extract_shape(array[i]))\n",
    "    return arr_feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def featureextraction_color(array):\n",
    "    arr_feature = []\n",
    "    for i in range(np.shape(array)[0]):\n",
    "        arr_feature.append(extract_color(array[i]))\n",
    "    return arr_feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, y_train = load_dataset(\"Dataset2/Train/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_texture = preprocessing_texture(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_shape = preprocessing_shape(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_color = preprocessing_color(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_texture_feature = featureextraction_texture(X_train_texture)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_shape_feature = featureextraction_shape(X_train_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_color_feature = featureextraction_color(X_train_color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf1 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf1.fit(X_train_texture_feature, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf2 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf2.fit(X_train_shape_feature, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf3 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf3.fit(X_train_color_feature, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_texture_shape = np.concatenate((X_train_texture_feature,X_train_shape_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf4 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf4.fit(X_train_texture_shape, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_texture_color = np.concatenate((X_train_texture_feature,X_train_color_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf5 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf5.fit(X_train_texture_color, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_shape_color = np.concatenate((X_train_shape_feature,X_train_color_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf6 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf6.fit(X_train_shape_color, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_train_feature = np.concatenate((X_train_texture_feature,X_train_shape_feature,X_train_color_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
       "            oob_score=False, random_state=0, verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf7 = RandomForestClassifier(n_estimators=100, random_state=0)\n",
    "clf7.fit(combined_train_feature, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total running time of training scheme :  3052.5215470790863  second\n"
     ]
    }
   ],
   "source": [
    "end1 = time.time()\n",
    "print(\"Total running time of training scheme : \",end1-start1,\" second\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> TEST SCHEME </h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "start2 = time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test, y_test = load_dataset(\"Dataset2/Test/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_texture = preprocessing_texture(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_shape = preprocessing_shape(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_color = preprocessing_color(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_texture_feature = featureextraction_texture(X_test_texture)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_shape_feature = featureextraction_shape(X_test_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_color_feature = featureextraction_color(X_test_color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi TEKSTUR :  61.35330170027679\n"
     ]
    }
   ],
   "source": [
    "result1 = clf1.predict(X_test_texture_feature) \n",
    "print(\"Akurasi TEKSTUR : \", accuracy_score(y_test, result1) * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi BENTUK :  50.06425464610518\n"
     ]
    }
   ],
   "source": [
    "result2 = clf2.predict(X_test_shape_feature) \n",
    "print(\"Akurasi BENTUK : \", accuracy_score(y_test, result2)*100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi WARNA :  70.1067615658363\n"
     ]
    }
   ],
   "source": [
    "result3 = clf3.predict(X_test_color_feature) \n",
    "print(\"Akurasi WARNA : \", accuracy_score(y_test, result3) * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_texture_shape = np.concatenate((X_test_texture_feature,X_test_shape_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi TEKSTUR + BENTUK :  78.98873072360617\n"
     ]
    }
   ],
   "source": [
    "result4 = clf4.predict(X_test_texture_shape) \n",
    "print(\"Akurasi TEKSTUR + BENTUK : \", accuracy_score(y_test, result4) * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_texture_color = np.concatenate((X_test_texture_feature,X_test_color_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi TEKSTUR + WARNA :  93.1395808620008\n"
     ]
    }
   ],
   "source": [
    "result5 = clf5.predict(X_test_texture_color) \n",
    "print(\"Akurasi TEKSTUR + WARNA : \", accuracy_score(y_test, result5) * 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>8.2.3 Klasifikasi Bentuk + Warna</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_shape_color = np.concatenate((X_test_shape_feature,X_test_color_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi BENTUK + WARNA :  88.70601028074337\n"
     ]
    }
   ],
   "source": [
    "result6 = clf6.predict(X_test_shape_color) \n",
    "print(\"Akurasi BENTUK + WARNA : \", accuracy_score(y_test, result6) * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_test_feature = np.concatenate((X_test_texture_feature,X_test_shape_feature,X_test_color_feature),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi GABUNGAN :  94.34064847765914\n"
     ]
    }
   ],
   "source": [
    "result7 = clf7.predict(combined_test_feature) \n",
    "print(\"Akurasi GABUNGAN : \", accuracy_score(y_test, result7) * 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total running time of test scheme :  307.8309528827667  second\n"
     ]
    }
   ],
   "source": [
    "end2 = time.time()\n",
    "print(\"Total running time of test scheme : \",end2-start2,\" second\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> OVERALL RUNNING TIME </h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total running time of all scheme :  650.378308057785  second\n"
     ]
    }
   ],
   "source": [
    "print(\"Total running time of all scheme : \",end2-start1,\" second\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "start3 = time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1> Validation Scheme </h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "def randomize(arrayA,arrayB):\n",
    "    randPerm = np.random.permutation(np.shape(arrayA)[0])\n",
    "    newArrayA = []\n",
    "    newArrayB = []\n",
    "    for i in range(0,len(randPerm)):\n",
    "        newArrayA.append(arrayA[randPerm[i]])\n",
    "        newArrayB.append(arrayB[randPerm[i]])\n",
    "    return np.array(newArrayA),np.array(newArrayB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "def KFoldCrossValidation(K,X,y):\n",
    "    scores = []\n",
    "    X,y = randomize(X,y)\n",
    "    clf = RandomForestClassifier(n_estimators=100,random_state=0)\n",
    "    cv = KFold(n_splits=K)\n",
    "    for train_index, test_index in cv.split(X):\n",
    "        X_train, X_test = X[train_index], X[test_index]\n",
    "        y_train, y_test = y[train_index], y[test_index]\n",
    "        \n",
    "        clf.fit(X_train, y_train)\n",
    "        result = clf.predict(X_test)\n",
    "        scores.append(accuracy_score(y_test, result))\n",
    "        \n",
    "    return np.mean(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI GABUNGAN:  99.8048399687744  %\n"
     ]
    }
   ],
   "source": [
    "validasi1 = KFoldCrossValidation(3,combined_train_feature,y_train)\n",
    "print(\"Akurasi VALIDASI GABUNGAN: \",validasi1*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI TEKSTUR:  81.10200364298726  %\n"
     ]
    }
   ],
   "source": [
    "validasi2 = KFoldCrossValidation(3,X_train_texture_feature,y_train)\n",
    "print(\"Akurasi VALIDASI TEKSTUR: \",validasi2*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI SHAPE:  83.50084569346863  %\n"
     ]
    }
   ],
   "source": [
    "validasi3 = KFoldCrossValidation(3,X_train_shape_feature,y_train)\n",
    "print(\"Akurasi VALIDASI SHAPE: \",validasi3*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI WARNA:  90.36397345823576  %\n"
     ]
    }
   ],
   "source": [
    "validasi4 = KFoldCrossValidation(3,X_train_color_feature,y_train)\n",
    "print(\"Akurasi VALIDASI WARNA: \",validasi4*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI TEKSTUR+BENTUK:  97.23360655737706  %\n"
     ]
    }
   ],
   "source": [
    "validasi5 = KFoldCrossValidation(3,X_train_texture_shape,y_train)\n",
    "print(\"Akurasi VALIDASI TEKSTUR+BENTUK: \",validasi5*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI TEKSTUR+WARNA:  99.37386156648452  %\n"
     ]
    }
   ],
   "source": [
    "validasi6 = KFoldCrossValidation(3,X_train_texture_color,y_train)\n",
    "print(\"Akurasi VALIDASI TEKSTUR+WARNA: \",validasi6*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Akurasi VALIDASI BENTUK+WARNA:  99.14454852979443  %\n"
     ]
    }
   ],
   "source": [
    "validasi7 = KFoldCrossValidation(3,X_train_shape_color,y_train)\n",
    "print(\"Akurasi VALIDASI BENTUK+WARNA: \",validasi7*100,\" %\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "end3 = time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total running time of validation scheme :  494.83908009529114  second\n"
     ]
    }
   ],
   "source": [
    "print(\"Total running time of validation scheme : \",end3-start3,\" second\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
